package dev.ideas.funkyj.group;

import dev.ideas.funkyj.function.F;
import dev.ideas.funkyj.function.F2;
import dev.ideas.funkyj.sequence.Sequence;
import dev.ideas.funkyj.tuple.Single;

import java.util.*;

import static dev.ideas.funkyj.sequence.Sequence.from;

/**
 */
public class Group1<G1, T> extends _Group.Node<G1, CachedSequence<T>, T> {
    private F<T, G1> f1;

    public Group1(Iterator<T> iterator, F<T, G1> f1) { super(iterator); this.f1 = f1; }
    Group1(_Group.Root<T> root, F<T, G1> f1) { super(root); this.f1 = f1; }

    @Override G1 key(T t) { return f1.apply(t); }
    @Override CachedSequence<T> create() { return new CachedSequence<T>(root); }

    public Sequence<T> select(final G1 g1) { return from(get(g1)); }

    //
    /* to reduce */
    public Group<G1, Single<T>> to(final F2<T, T, T> f) {
        return new Group<G1, Single<T>>() {
            @Override public Single<T> select(G1 g1) {
                return from(Group1.this.select(g1)).to(f);
            }
        };
    }
}
